package com.xu.demof.service;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import java.io.File;

@Service
public class InvoiceService {
    private static final String TESS_DATA_PATH = "path/to/tessdata";

    private ITesseract tesseract;

    public Map<String, String> extractInvoiceInfo(File imageFile) throws IOException, TesseractException {
//        Tesseract tesseract = new Tesseract();
//        tesseract.setDatapath(TESS_DATA_PATH);
//        tesseract.setLanguage("eng");
        // 创建 Tesseract 实例
        // 初始化 Tesseract OCR
        tesseract = new Tesseract();
        // 设置训练数据的路径（如果你的 traineddata 文件放在 D:\home\testwork\demoF\traineddata）
        tesseract.setDatapath("D:\\home\\testwork\\demoF\\traineddata");
        // 设置语言，例如中文
        tesseract.setLanguage("chi_sim");

        // 设置分辨率（使用 setTessVariable 方法）
        tesseract.setTessVariable("user_defined_dpi", "300");

        // 设置字符白名单
//        tesseract.setTessVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");


        // OCR识别
        String text = tesseract.doOCR(imageFile);

        // 提取关键信息
//        Map<String, String> keyInfo = extractKeyInfo(text);
        Map<String, String> keyInfo = extractKeyInfo2(text);

        // 删除临时文件
        FileUtils.forceDelete(imageFile);

        return keyInfo;
    }

    private Map<String, String> extractKeyInfo(String text) {
        Map<String, String> keyInfo = new HashMap<>();

        // 假设发票号是以"INV"开头的一串数字
        Pattern invoiceNumberPattern = Pattern.compile("INV\\d+");
        Matcher invoiceNumberMatcher = invoiceNumberPattern.matcher(text);
        if (invoiceNumberMatcher.find()) {
            keyInfo.put("number", invoiceNumberMatcher.group());
        }

        // 假设日期格式为"YYYY-MM-DD"
        Pattern datePattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
        Matcher dateMatcher = datePattern.matcher(text);
        if (dateMatcher.find()) {
            keyInfo.put("date", dateMatcher.group());
        }

        // 假设金额以"$"符号开头
        Pattern amountPattern = Pattern.compile("\\$\\d+(\\.\\d{2})?");
        Matcher amountMatcher = amountPattern.matcher(text);
        if (amountMatcher.find()) {
            keyInfo.put("amount", amountMatcher.group());
        }

        return keyInfo;
    }

    private Map<String, String> extractKeyInfo2(String text) {
        Map<String, String> keyInfo = new HashMap<>();

        // 以冒号（":"）隔开的格式进行提取
        Pattern pattern = Pattern.compile("(.*?):(.*)");
        Matcher matcher = pattern.matcher(text);

        while (matcher.find()) {
            String key = matcher.group(1).trim(); // 提取冒号前的内容作为key
            String value = matcher.group(2).trim(); // 提取冒号后的内容作为value
            keyInfo.put(key, value);
        }

        return keyInfo;
    }

}
